package com.niuwa.p2p.service.impl.channel;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.niuwa.p2p.core.utils.StringUtil;
import com.niuwa.p2p.dao.channel.BlacklistCheckLogDao;
import com.niuwa.p2p.dao.channel.CertCheckLogDao;
import com.niuwa.p2p.dao.channel.ChannelInfoCheckDao;
import com.niuwa.p2p.dao.channel.CorpCheckLogDao;
import com.niuwa.p2p.dao.channel.CorporationDao;
import com.niuwa.p2p.dao.credit.CompanyCourtInfoDao;
import com.niuwa.p2p.dao.credit.CorpBaseNationalInfoDao;
import com.niuwa.p2p.dao.credit.PersonCourtInfoDao;
import com.niuwa.p2p.entity.channel.BlacklistCheckLog;
import com.niuwa.p2p.entity.channel.CertCheckLog;
import com.niuwa.p2p.entity.channel.CorpCheckLog;
import com.niuwa.p2p.entity.channel.Corporation;
import com.niuwa.p2p.entity.credit.CompanyCourtInfo;
import com.niuwa.p2p.entity.credit.CorpBaseNationalInfo;
import com.niuwa.p2p.entity.credit.PersonCourtInfo;
import com.niuwa.p2p.service.channel.IChannelInfoCheckService;
import com.niuwa.p2p.vo.channel.ChannelInfoCheckVo;

@Service
public class ChannelInfoCheckService implements IChannelInfoCheckService {

	@Autowired
	private CorporationDao corporationDao;
	@Autowired
	private ChannelInfoCheckDao channelInfoCheckDao;
	@Autowired
	private CertCheckLogDao certCheckLogDao;
	@Autowired
	private CorpCheckLogDao corpCheckLogDao;
	@Autowired
	private BlacklistCheckLogDao blacklistCheckLogDao;
	@Autowired
	private CompanyCourtInfoDao companyCourtInfoDao;
	@Autowired
	private CorpBaseNationalInfoDao corpBaseNationalInfoDao;
	@Autowired
	private PersonCourtInfoDao personCourtInfoDao;

	@Override
	public Corporation getCorporationInfo(String corpCode) {
		return corporationDao.selectByPrimaryKey(corpCode);
	}

	@Override
	public ChannelInfoCheckVo getCorpLegalMan(String corpCode) {
		return channelInfoCheckDao.selectCorpLegalMan(corpCode);
	}

	@Override
	public ChannelInfoCheckVo getCorpGeneralManager(String corpCode) {
		return channelInfoCheckDao.selectCorpGeneralManager(corpCode);
	}

	@Override
	public ChannelInfoCheckVo getCorp(String corpCode) {
		return channelInfoCheckDao.selectCorp(corpCode);
	}

	@Override
	public List<ChannelInfoCheckVo> getShareholder(String corpCode) {
		List<ChannelInfoCheckVo> shareHolders = new ArrayList<ChannelInfoCheckVo>();
		shareHolders.addAll(channelInfoCheckDao.selectPersonShareholder(corpCode));
		shareHolders.addAll(channelInfoCheckDao.selectCorpShareholder(corpCode));
		return shareHolders;
	}

	@Override
	public List<ChannelInfoCheckVo> getPersonShareholder(String corpCode) {
		return channelInfoCheckDao.selectPersonShareholder(corpCode);
	}

	@Override
	public List<ChannelInfoCheckVo> getCorpShareholder(String corpCode) {
		return channelInfoCheckDao.selectCorpShareholder(corpCode);
	}

	@Override
	public void saveCorpCheckLog(String corpName, long userId) {
		CorpCheckLog corpCheckLog = new CorpCheckLog();
		corpCheckLog.setCorpName(corpName);
		corpCheckLog.setCheckTime(new Date());
		corpCheckLog.setValidTerm(30);
		corpCheckLog.setOperator(userId);
		corpCheckLog.setCheckStatus("通过");
		corpCheckLogDao.insert(corpCheckLog);
	}

	@Override
	public void saveCertCheckLog(String name, String certNo, long userId) {
		CertCheckLog certCheckLog = new CertCheckLog();
		certCheckLog.setName(name);
		certCheckLog.setCertNo(certNo);
		certCheckLog.setCheckTime(new Date());
		certCheckLog.setValidTerm(180);
		certCheckLog.setOperator(userId);
		certCheckLog.setCheckStatus("通过");
		certCheckLogDao.insert(certCheckLog);
	}

	@Override
	public List<ChannelInfoCheckVo> selectBlackListManager(String corpCode) {
		return channelInfoCheckDao.selectBlackListManager(corpCode);
	}

	@Override
	public List<ChannelInfoCheckVo> selectBlackListPersonShareholder(String corpCode) {
		return channelInfoCheckDao.selectBlackListPersonShareholder(corpCode);
	}

	@Override
	public List<ChannelInfoCheckVo> selectBlackListCorpShareholder(String corpCode) {
		return channelInfoCheckDao.selectBlackListCorpShareholder(corpCode);
	}

	@Override
	public List<ChannelInfoCheckVo> selectBlackList(String corpCode, long userId) {
		// 查询联络人、法人、总经理信息
		ChannelInfoCheckVo corp = this.getCorp(corpCode);
		ChannelInfoCheckVo legalMan = this.getCorpLegalMan(corpCode);
		ChannelInfoCheckVo generalManager = this.getCorpGeneralManager(corpCode);
		List<ChannelInfoCheckVo> personShareholder = channelInfoCheckDao.selectPersonShareholder(corpCode);
		List<ChannelInfoCheckVo> corpShareholder = channelInfoCheckDao.selectCorpShareholder(corpCode);

		List<ChannelInfoCheckVo> results = new ArrayList<ChannelInfoCheckVo>();
		results.add(corp);
		results.add(legalMan);
		results.add(generalManager);
		results.addAll(personShareholder);
		results.addAll(corpShareholder);

		List<ChannelInfoCheckVo> ret = new ArrayList<ChannelInfoCheckVo>();
		Set<String> certNoSet = new HashSet<String>();
		Set<String> mobileSet = new HashSet<String>();
		Set<String> nameSet = new HashSet<String>();
		for (ChannelInfoCheckVo result : results) {
			BlacklistCheckLog log = new BlacklistCheckLog();

			if (this.checkBlackList(result) > 0) {
				if (StringUtil.isEmpty(result.getCertNo()) && StringUtil.isEmpty(result.getMobile())) {
					if (!nameSet.contains(result.getName())) {
						ret.add(result);
						nameSet.add(result.getName());
					}
				} else {
					if (certNoSet.contains(result.getCertNo())) {
						continue;
					}
					if (mobileSet.contains(result.getMobile())) {
						continue;
					}
					mobileSet.add(result.getMobile());
					certNoSet.add(result.getCertNo());
					ret.add(result);

					log.setCertNo(result.getCertNo());
					log.setCertType(result.getCertType());
					log.setCheckTime(new Date());
					log.setName(result.getName());
					log.setCheckStatus("NO_PASS");
					log.setOperator(userId);
					blacklistCheckLogDao.insert(log);

				}
			} else {
				log.setCertNo(result.getCertNo());
				log.setCertType(result.getCertType());
				log.setCheckTime(new Date());
				log.setName(result.getName());
				log.setCheckStatus("PASSED");
				log.setOperator(userId);
				blacklistCheckLogDao.insert(log);
			}
		}
		return ret;
	}

	@Override
	public int checkBlackList(ChannelInfoCheckVo channelInfoCheckVo) {
		return channelInfoCheckDao.checkBlackList(channelInfoCheckVo);
	}

	@Override
	public CompanyCourtInfo getCorpCourtInfo(String corpName) {
		return companyCourtInfoDao.selectByCorpName(corpName);
	}

	@Override
	public CorpBaseNationalInfo getCorpBaseNationalInfo(String corpName) {
		return corpBaseNationalInfoDao.selectByCorpName(corpName);
	}

	@Override
	public PersonCourtInfo getPersonCourtInfo(String name, String documentNo) {
		return personCourtInfoDao.selectByPersonInfo(name, documentNo);
	}

	@Override
	public void checkPersonCredit(ChannelInfoCheckVo person, List<ChannelInfoCheckVo> court) {
		if (StringUtil.isEmpty(person.getCheckStatus())) {

		} else {
			// 查询过
			PersonCourtInfo personCourtInfo = this.getPersonCourtInfo(person.getName(), person.getCertNo());
			// 没有被法院执行信息
			if (null == personCourtInfo) {

			} else {
				// 显示法院的执行信息
				court.add(person);
			}
		}
	}

	@Override
	public void checkCredit(List<Object> credits, List<ChannelInfoCheckVo> court, List<ChannelInfoCheckVo> aic) {
		ChannelInfoCheckVo corp = (ChannelInfoCheckVo) credits.get(0);
		ChannelInfoCheckVo legalMan = (ChannelInfoCheckVo) credits.get(1);
		ChannelInfoCheckVo generalManager = (ChannelInfoCheckVo) credits.get(2);
		List<ChannelInfoCheckVo> personShareholderList = (List<ChannelInfoCheckVo>) credits.get(3);
		List<ChannelInfoCheckVo> corpShareholderList = (List<ChannelInfoCheckVo>) credits.get(4);

		checkCorpCredit(corp, court, aic);
		checkPersonCredit(legalMan, court);
		checkPersonCredit(generalManager, court);
		for (ChannelInfoCheckVo personShareholder : personShareholderList) {
			checkPersonCredit(personShareholder, court);
		}
		for (ChannelInfoCheckVo corpShareholder : corpShareholderList) {
			checkCorpCredit(corpShareholder, court, aic);
		}
	}

	@Override
	public void checkCorpCredit(ChannelInfoCheckVo corp, List<ChannelInfoCheckVo> court, List<ChannelInfoCheckVo> aic) {
		// 如果没有查询过
		if (StringUtil.isEmpty(corp.getCheckStatus())) {

		} else {
			// 查询过
			CompanyCourtInfo companyCourtInfo = this.getCorpCourtInfo(corp.getName());
			// 没有被法院执行信息
			if (null == companyCourtInfo) {

			} else {
				// 显示法院的执行信息
				court.add(corp);
			}

			// 查询该企业的工商信息
			CorpBaseNationalInfo corpBaseNationalInfo = this.getCorpBaseNationalInfo(corp.getName());
			// 没有工商信息
			if (null == corpBaseNationalInfo || StringUtil.isEmpty(corpBaseNationalInfo.getRegisterNo())) {
				aic.add(corp);
			} else {
				// 显示工商信息
			}
		}
	}
}
